import { _decorator, Component, Node, Label, director } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('hallSceneMgr')
export class hallSceneMgr extends Component {
    @property(Node) 
    public createRoom:Node 
    @property(Node) 
    public joinRoom:Node 
    @property(Label) 
    public nameLabel:Label 
    @property(Label) 
    public IDLabel:Label 

    start() {
        this._init();
    }

    update(deltaTime: number) {
        
    }


    public _init(){
        globalThis._eventTarget.on('create_room',this.onCreateRoom,this);
        globalThis._eventTarget.on('join_room',this.onJoinRoom,this);
        this.nameLabel.string = globalThis._userInfo.user_name;
        this.IDLabel.string = 'ID:' + globalThis._userInfo.user_id;
    }

    //创建房间按钮点击的逻辑
    public onCreateRoomBtnClicked(){
        this.createRoom.active = true;
    }

    public onJoinRoomBtnClicked(){
        this.joinRoom.active = true;
    }

    public onCreateRoom(room_id){
        if(room_id === 0){
            console.log('创建房间失败');
        }else{
            console.log('创建房间服务端返回的房间号为：',room_id);
            globalThis._userInfo.room_id = room_id;
            director.loadScene("PSZ");
            //由大厅场景跳转到游戏场景
        }
    }

    public onJoinRoom(roomData){
        if(!roomData){
            console.log('未查询到此房间');
            return;
        }
        let result = JSON.parse(roomData);
        console.log(result);

        globalThis._userInfo.room_id = result.room_id;
        director.loadScene("PSZ");
    }
}

